package com.enlighten.coinDataFetcher;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CircleDrawer {

	private int radius;
	private int squaredRadius;

	public CircleDrawer(int radius) {
		this.radius = radius;
		if ( radius % 2 == 0 )
			this.radius++;
		squaredRadius = radius*radius;
	}

	public static void main(String[] args) {
		for(int i = 2; i < 200 ; i++){
			CircleDrawer circleDrawer = new CircleDrawer(i);
			
			circleDrawer.draw(i+"b.png");
		}
		double a=0;
		double b=20;
		for(int i=0; i < 20 ; i++){
			double nuevo = Math.sqrt(2*Math.pow(b, 2) - Math.pow(a, 2));
			System.out.println(a+" - "+b+" - "+nuevo);
			a=b;
			b=nuevo;
		}
	}

	private void draw(String filename) {
		int imgSize = getImageSize();
		BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
		for(int i=0; i < imgSize ; i++)
			for(int j=0; j < imgSize ; j++){
				bufferedImage.setRGB(i, j, getColorFor(i,j));
			}
		try {
			ImageIO.write(bufferedImage, "PNG", new File(filename));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private int getColorFor(int i, int j) {
		int translatedX = i - radius;
		int translatedY = j - radius;
		int squaredDistance = translatedX*translatedX + translatedY*translatedY;
		if ( shouldPaint(squaredDistance) )
			return Color.BLACK.getRGB();
		return Color.WHITE.getRGB();
	}

	private boolean shouldPaint(int squaredDistance) {
//		return (squaredDistance > squaredRadius - radius - 1) && (squaredDistance < squaredRadius + radius + 1);
		return (squaredDistance > squaredRadius - radius + 1/4) && (squaredDistance < squaredRadius + radius + 1/4);
	}

	private int getImageSize() {
		return radius*2+1;
	}
}
